package cn.zyl.demo.undertow.tests;

import io.undertow.io.IoCallback;
import io.undertow.io.Sender;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.server.handlers.resource.RangeAwareResource;
import io.undertow.server.handlers.resource.Resource;
import io.undertow.util.ETag;
import io.undertow.util.MimeMappings;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Date;
import java.util.List;

/**
 * JspResourceLoader 类说明: 参考<code>io.undertow.servlet.test.util.TestResourceLoader</code>
 *
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-07
 */
public class JspResourceLoader extends ClassPathResourceManager {
    public JspResourceLoader(Class<?> testClass) {
        super(testClass.getClassLoader(), testClass.getPackage().getName().replace(".", "/"));
    }
    @Override
    public Resource getResource(String path) throws IOException {
        Resource delegate = super.getResource(path);
        return delegate == null ? null : new JspResourceLoader.TestResource(delegate);
    }
    private static class TestResource implements RangeAwareResource {
        private final Resource delegate;

        TestResource(Resource delegate) {
            this.delegate = delegate;
        }
        @Override
        public String getPath() {
            return this.delegate.getPath();
        }
        @Override
        public Date getLastModified() {
            return new Date(this.delegate.getLastModified().getTime() + 20L);
        }
        @Override
        public String getLastModifiedString() {
            return this.delegate.getLastModifiedString();
        }
        @Override
        public ETag getETag() {
            return this.delegate.getETag();
        }
        @Override
        public String getName() {
            return this.delegate.getName();
        }
        @Override
        public boolean isDirectory() {
            return this.delegate.isDirectory();
        }
        @Override
        public List<Resource> list() {
            return this.delegate.list();
        }
        @Override
        public String getContentType(MimeMappings mimeMappings) {
            return this.delegate.getContentType(mimeMappings);
        }
        @Override
        public void serve(Sender sender, HttpServerExchange exchange, IoCallback completionCallback) {
            this.delegate.serve(sender, exchange, completionCallback);
        }
        @Override
        public Long getContentLength() {
            return this.delegate.getContentLength();
        }
        @Override
        public String getCacheKey() {
            return this.delegate.getCacheKey();
        }
        @Override
        public File getFile() {
            return this.delegate.getFile();
        }
        @Override
        public Path getFilePath() {
            return this.delegate.getFilePath();
        }
        @Override
        public File getResourceManagerRoot() {
            return this.delegate.getResourceManagerRoot();
        }
        @Override
        public Path getResourceManagerRootPath() {
            return this.delegate.getResourceManagerRootPath();
        }
        @Override
        public URL getUrl() {
            return this.delegate.getUrl();
        }
        @Override
        public void serveRange(Sender sender, HttpServerExchange exchange, long start, long end, IoCallback completionCallback) {
            ((RangeAwareResource)this.delegate).serveRange(sender, exchange, start, end, completionCallback);
        }
        @Override
        public boolean isRangeSupported() {
            return this.delegate instanceof RangeAwareResource && ((RangeAwareResource)this.delegate).isRangeSupported();
        }
    }
}
